﻿using Microsoft.Reporting.WebForms;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Util
{
    public static class MyExtensions
    {
        public static string ToYMD(this DateTime d)
        {
            return d.ToString("yyyy-MM-dd");
        }

        public static string ToYMDHM(this DateTime d)
        {
            return d.ToString("yyyy-MM-dd HH:mm");
        }

        public static string ToYMDHMS(this DateTime d)
        {
            return d.ToString("yyyy-MM-dd HH:mm:ss");
        }

        public static string ToYMD(this DateTime? d)
        {
            if (d == null) return String.Empty;
            return d.Value.ToString("yyyy-MM-dd");
        }

        public static string ToYMDHM(this DateTime? d)
        {
            if (d == null) return String.Empty;
            return d.Value.ToString("yyyy-MM-dd HH:mm");
        }

        public static string ToYMDHMS(this DateTime? d)
        {
            if (d == null) return String.Empty;
            return d.Value.ToString("yyyy-MM-dd HH:mm:ss");
        }

        public static string Left(this string s, int length)
        {
            if (s.Length <= length) return s;
            return s.Substring(0, length);
        }

        public static string Right(this string s, int length)
        {
            int index = Math.Max(0, s.Length - length);
            return s.Substring(index);
        }

        public static void 데이터바인드(this ReportViewer reportViewer, object data)
        {
            reportViewer.ShowReportBody = true;
            var ds = new ReportDataSource("DataSet1", data);
            reportViewer.LocalReport.DataSources.Clear();
            reportViewer.LocalReport.DataSources.Add(ds);
            reportViewer.LocalReport.Refresh();
        }

        public static MvcHtmlString MyAction(this UrlHelper url, string action, string controller, object param)
        {
            return MvcHtmlString.Create(url.Action(action, controller, param));
        }

        public static MvcHtmlString MyAction(this UrlHelper url, string action, string controller)
        {
            return MvcHtmlString.Create(url.Action(action, controller));
        }

    }
}